if (!UT) var UT = {};
UT.URLDecode = function(Str) {
    if (Str == null || Str == "") {
        return "";
    }
    var newStr = "";
    function toCase(sStr) {
        return sStr.toString(16).toUpperCase();
    };
    for (var i = 0,
    ichar, len = Str.length; i < len;) {
        if (Str.charAt(i) == "%") {
            ichar = Str.charAt(i + 1);
            if (ichar.toLowerCase() == "e") {
                newStr += String.fromCharCode(((parseInt("0x" + Str.substr(i + 1, 2)) - 0xE0) * 0x1000) + ((parseInt("0x" + Str.substr(i + 4, 2)) - 0x80) * 0x40) + (parseInt("0x" + Str.substr(i + 7, 2)) - 0x80));
                i += 9;
            } else if (ichar.toLowerCase() == "c" || ichar.toLowerCase() == "d") {
                newStr += String.fromCharCode(((parseInt("0x" + Str.substr(i + 1, 2)) - 0xC0) * 0x40) + parseInt("0x" + Str.substr(i + 4, 2)) - 0x80);
                i += 6;
            } else {
                newStr += String.fromCharCode(parseInt("0x" + Str.substr(i + 1, 2)));
                i += 3;
            }
        } else {
            newStr += Str.charAt(i).replace(/\+/, " ");
            i++;
        }
    }
    return newStr;
};
UT.URLToParams = function(params) {
    try {
        var cmdMap = params.split("&"),
        cmdParams = [],
        temp;
        for (var i = 0; i < cmdMap.length; i++) {
            temp = cmdMap[i].split("=");
            cmdParams[temp[0]] = temp[1];
        }
        return cmdParams;
    } catch(e) {
        E.report(LIM.localRes["paramModifyError"], '1206');
        return [];
    }
};
UT.ParamsToUrl = function(params) {
    var url = "";
    for (var name in params) {
        if (name && params[name] && typeof params[name] != "function") {
            url += name + "=" + params[name] + "&";
        }
    }
    if (url != "") {
        url = url.substring(0, url.length - 1);
    }
    return url;
};
UT.addEvent = function(element, eventName, eventFunction) {
    try {
        if (document.attachEvent) {
            element.attachEvent("on" + eventName, eventFunction);
        } else {
            element.addEventListener(eventName, eventFunction, false);
        }
    } catch(e) {}
};
if (!objId) var objId = function(id) {
    return document.getElementById(id);
};
function showFunc() {
    var funcString = '';
    funcString = '<div class="leftFunc" style="display: none;" title="' + LIM.resource["changfunc_tip_0"] + '" id="leftfunctions"><a href="#" onClick="lefFunc();"><img src="refactor/style/images/leftF.png"/></a></div>';
    if (LIM.config.robotService == 'true'&&LIM.config.skin=="new-custom-tangerine") {
        funcString += '<div class="topItemsContainer" style="display: none;" sel="0"><span class="interval"></span><span class="containerItemsb"><span id="jumperRobot"></span><span class="containerItemst">' + LIM.resource["changfunc_tip_9"] + '</span></span></div>';
    }
    if (LIM.config.staffService == 'true') {
    	var forbid = (LIM.config.ipProhibit == 'true' || LIM.config.userProhibit == 'true')?'lim:forbid=1':'';
        funcString += '<div class="topItemsContainer" style="display: none;" sel="0"><span class="interval"></span><span class="containerItemsb"><span id="jumper" ' + forbid + '></span><span class="containerItemst">' + LIM.resource["changfunc_tip_1"] + '</span></span></div>';
    }
    if (LIM.config.onlineLeaveword == 'true') {
        funcString += '<div class="topItemsContainer" style="display: none;" sel="0"><span class="interval"></span><span class="containerItemsb"><span id="LMessage"></span><span class="containerItemst">' + LIM.resource["changfunc_tip_2"] + '</span></span></div>';
    }
    if (LIM.config.complaint == 'true') {
        funcString += '<div class="topItemsContainer" style="display: none;" sel="0"><span class="interval"></span><span class="containerItemsb"><span id="complaint"></span><span class="containerItemst">' + LIM.resource["changfunc_tip_3"] + '</span></span></div>';
    }
    if (LIM.config.selfSeachServiceEnable == 'true') {
        funcString += '<div class="topItemsContainer" style="display: none;" sel="0"><span class="interval"></span><span class="containerItemsb"><span id="selfSeachService"></span><span class="containerItemst">' + LIM.resource["changfunc_tip_4"] + '</span></span></div>';
    }
    if (LIM.config.intelligentSearch == 'true') {
        funcString += '<div class="topItemsContainer" style="display: none;" sel="0"><span class="interval"></span><span class="containerItemsb"><span id="intelligentSearch"></span><span class="containerItemst">' + LIM.resource["changfunc_tip_5"] + '</span></span></div>';
    }
    if (LIM.config.isVideo == '1' && LIM.config['showRightWindow'] != 0) {
        funcString += '<div class="topItemsContainer" style="display: none;" sel="0"><span class="interval"></span><span class="containerItemsb"><span id="live800_video"></span><span class="containerItemst">' + LIM.resource["changfunc_tip_8"] + '</span></span></div>';
    }
    if (LIM.config.userDefinedButton == 'true') { //允许添加自定义按钮
        var json = eval('(' + LIM.config.userDefinedButtonJson + ')');
        for (var btn in json) {
            funcString += '<div class="topItemsContainer" style="display: none;" sel="0"><span class="interval"></span><span class="containerItemsb"><span id="udButton' + json[btn]['id'] + '" style="display:block;background-image:url(' + urlDecode(json[btn]['pic']) + ');background-repeat: no-repeat;background-position: center center;width:48px;margin:0px auto;height:48px;_background:none;_filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(' + 'enabled=true, sizingMethod=scale, src=\'' + urlDecode(json[btn]['pic']) + '\');"></span><span class="containerItemst">' + urlDecode(json[btn]['name']) + '</span></span></div>';
        }
    }
    funcString += '<div class="rightFunc" style="display: none;" sel="0" title="' + LIM.resource["changfunc_tip_0"] + '" id="rightfunctions"><a href="#" onClick="rightFunc();" ><img src="refactor/style/images/rightF.png"/></a></div><div class="clo" style="display: none;" sel="0"></div>';
    var d = document,
    element = d.getElementById("functionContainer");
    element.innerHTML = funcString;
    var elements = element.getElementsByTagName("div");
    for (var i = 1,
    lenth = elements.length - 2; i < lenth; i++) {
        var span = elements[i].getElementsByTagName("span")[1]; (function(span) {
            UT.addEvent(span, 'mouseover',
            function() {});
            UT.addEvent(span, 'mouseout',
            function() {});
        })(span);
    }
    if ("8900" == LIM.config.companyID) {
        objId('lefttop').style.width = lenth * 80 + 'px';
        for (var i = 1,
        lenth = elements.length - 2; i < lenth; i++) {
            elements[i].style.display = "";
            elements[1].getElementsByTagName('span')[0].style.display = 'none';
        }
    } else {
        if (lenth >= 7) {
            objId('lefttop').style.width = '423px';
        } else {
            objId('lefttop').style.width = (lenth - 1) * 80 + 'px';
        }
        if (elements.length < 9) {
            for (var i = 1,
            lenth = elements.length - 2; i < lenth; i++) {
                elements[i].style.display = "";
                elements[1].getElementsByTagName('span')[0].style.display = 'none';
            }
        } else if (elements.length > 8) {
            for (var i = 1,
            lenth = 6; i < lenth; i++) {
                elements[i].style.display = "";
            }
            elements[1].getElementsByTagName('span')[0].style.display = 'none';
            elements[elements.length - 2].style.display = "";
            elements[5].setAttribute("sel", "1");
        }
    }
    if (objId('jumper')) {
        if (LIM.config.serviceType == 0 || LIM.config.serviceType == 2 || LIM.config.LMessagePage) {
            objId('jumper').disabled = true;
            objId('jumper').style.cursor = "no-drop";
        }
    }
    if (objId('LMessage')) {
        if (LIM.config.LMessagePage) {
            objId('LMessage').disabled = true;
            objId('LMessage').style.cursor = "no-drop";
        }
    }
    if(objId('live800_video')){
    	if("undefined"!=typeof(Live800Robot)){
    		objId('live800_video').disabled = true;
    		objId('live800_video').style.cursor = "no-drop";
    	}
    }
    if(objId('jumperRobot')){
    	if(LIM.config.robot.enable){
    		objId('jumperRobot').disabled = true;
    		objId('jumperRobot').style.cursor = "no-drop";
    	}
    }
    if (!LIM.config.LMessagePage) {
        loadFaq();
    }
    FuncEvent();
};
UT.addEvent(window, 'load', showFunc);

function rightFunc() {
    var d = document,
    element = d.getElementById("rightfunctions"),
    par = element.parentNode,
    element = d.getElementById("functionContainer"),
    elements = element.getElementsByTagName("div");
    var k;
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].getAttribute("sel") == 1) {
            k = i;
            elements[i].setAttribute("sel", "0");
        }
    }
    elements[k + 1].setAttribute("sel", "1");
    elements[0].style.display = "";
    elements[k - 4].style.display = "none";
    elements[k - 3].getElementsByTagName('span')[0].style.display = 'none';
    elements[k - 2].getElementsByTagName('span')[0].style.display = '';
    elements[k + 1].style.display = "";
    k += 1;
    if (k == elements.length - 3) {
        elements[elements.length - 2].style.display = "none";
    }
}
function lefFunc() {
    var d = document,
    element = d.getElementById("leftfunctions"),
    par = element.parentNode,
    element = d.getElementById("functionContainer"),
    elements = element.getElementsByTagName("div");
    var k;
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].getAttribute("sel") == 1) {
            k = i;
            elements[i].setAttribute("sel", "0");
        }
    }
    elements[k - 1].setAttribute("sel", "1");
    elements[elements.length - 2].style.display = "";
    elements[k].style.display = "none";
    elements[k - 5].style.display = "";
    elements[k - 5].getElementsByTagName('span')[0].style.display = 'none';
    elements[k - 4].getElementsByTagName('span')[0].style.display = '';
    k = k - 1;
    if (k == 5) {
        elements[0].style.display = "none";
    }
}
function selfQuery(){
 	if("undefined"==typeof(Live800Robot)){
 		robotLoadJs(LIM.config.robot.robotJs);
 	} else{
 		var selfSeachParms = UT.URLDecode(LIM.config["selfSeachParms"]);
 		var params = UT.URLToParams(selfSeachParms);
 		Live800Robot.ext.selfQuery(params);
 	}
 }
 
 function robotLoadJs(url){
		var s=document.createElement("script");
		s.setAttribute("type","text/javascript");
		s.setAttribute("src",url);
		document.getElementsByTagName("head")[0].appendChild(s);
	    if (document.all) {
	        s.onreadystatechange = function(){
	            var state = s.readyState;
	            if (state == "loaded" || state == "interactive" || state == "complete") {
	            	robotSelfQuery();
	            }
	        };
	    }
	    else {
	        s.onload = function(){
	        	robotSelfQuery();
	        };
	    }
	}
	
	function robotSelfQuery(){
		var _timer = setInterval(function(){
		 	if("undefined"!=typeof(Live800Robot) && "undefined"!=typeof(Live800Robot.ext)){
		 		var selfSeachParms = UT.URLDecode(LIM.config["selfSeachParms"]);
		 		var params = UT.URLToParams(selfSeachParms);
		 		Live800Robot.ext.selfQuery(params);
		 		clearInterval(_timer);
		 	}
		}, 50);
	 }
function FuncEvent() {
    if (objId('complaint')) {
        UT.addEvent(objId("complaint"), 'click',
        function() {
            LIM.vClickOnStatistics.submitInformation(13); //华为点击次数记录
            if (LIM.config.LMessagePage) {
                if (LIM.config.complaintLink == null || LIM.config.complaintLink == "") {
                    return;
                } else {
                    window.open(UT.URLDecode(LIM.config.complaintLink));
                }
            } else {
                if (LIM.config.complaintLink == null || LIM.config.complaintLink == "") {
                    LIM.config["leavewordEnable"] = false;
                    var LMUrl = getLeavewordMessageUrl();
                    if (LIM.config.skin.substr(0, 4) == "new-" && LIM.config["showRightWindow"] == 1) {
                        LIM.creatiframe({
                            url: LMUrl,
                            title: LIM.resource["changfunc_tip_3"]
                        });
                        LIM.config["leavewordEnable"] = true; //true?
                    } else {
                        if (confirm(getConfirmExit())) {
                            window.onbeforeunload = null;
                            window.onunload = null;
                            globalChatHandle.isClickLeaveButton = true;
                            closeChatClose();
                            LIM.config["leavewordEnable"] = true; //true?
                        }
                    }
                    return;
                } else {
                    if (LIM.config.skin.substr(0, 4) == "new-" && LIM.config["showRightWindow"] == 1) {
                        LIM.creatiframe({
                            url: UT.URLDecode(LIM.config.complaintLink),
                            title: LIM.resource["changfunc_tip_3"]
                        });
                    } else {
                        window.open(UT.URLDecode(LIM.config.complaintLink));
                    }
                    return;
                }
            }
        });
    }
    if (objId('intelligentSearch')) {
        UT.addEvent(objId('intelligentSearch'), 'click',
        function() {
            LIM.vClickOnStatistics.submitInformation(15); //华为点击次数记录
            if (LIM.config.LMessagePage) {
                if (LIM.config.intelligentSearchLink == null || LIM.config.intelligentSearchLink == "") {
                    return;
                } else {
                    window.open(UT.URLDecode(LIM.config.intelligentSearchLink));
                }
            } else {
                if (LIM.config.intelligentSearchLink == null || LIM.config.intelligentSearchLink == "") {
                    LIM.config["leavewordEnable"] = false;
                    var LMUrl = getLeavewordMessageUrl();
                    if (LIM.config.skin.substr(0, 4) == "new-" && LIM.config["showRightWindow"] == 1) {
                        LIM.creatiframe({
                            url: LMUrl,
                            title: LIM.resource["changfunc_tip_5"]
                        });
                        LIM.config["leavewordEnable"] = true; //true?
                    } else {
                        if (confirm(getConfirmExit())) {
                            window.onbeforeunload = null;
                            window.onunload = null;
                            globalChatHandle.isClickLeaveButton = true;
                            closeChatClose();
                            LIM.config["leavewordEnable"] = true; //true?
                        }
                    }
                    return;
                } else {
                    if (LIM.config.skin.substr(0, 4) == "new-" && LIM.config["showRightWindow"] == 1) {
                        LIM.creatiframe({
                            url: UT.URLDecode(LIM.config.intelligentSearchLink),
                            title: LIM.resource["changfunc_tip_5"]
                        });
                    } else {
                        window.open(UT.URLDecode(LIM.config.intelligentSearchLink));
                    }
                    return;
                }
            }
        });
    }
    if (objId('selfSeachService')) {
        UT.addEvent(objId('selfSeachService'), 'click',
        function() {
            selfQuery();
            LIM.vClickOnStatistics.submitInformation(14); //华为点击次数记录
        });
    }
    if (LIM.config.userDefinedButton == 'true') { //允许添加自定义按钮
        var json = eval('(' + LIM.config.userDefinedButtonJson + ')');
        for (var btn in json) {
            var udButtonId = json[btn]['id'];
            var btnObj = objId("udButton" + json[btn]['id']);
            var link = urlDecode(json[btn]['link']).replace(/&amp;/g,"&");
            var showType = json[btn]['showType'];
            var tabname = urlDecode(json[btn]['name']); (function(btnObj, udButtonId, link, tabname, showType) {
                UT.addEvent(btnObj, 'click',
                function() {
                    var img = new Image();
                    img.src = LIM.config["protocol"] + "://" + LIM.config.chatDirDomains + "/scripts/udButtonClickAction.jsp?configId=" + udButtonId + "&tm=" + new Date().getTime();
                    LIM.vClickOnStatistics.submitInformation(17, udButtonId); //华为点击次数记录
                    if (showType == 1 && LIM.config["showRightWindow"] == 1) {
                        LIM.creatiframe({
                            url: link,
                            title: tabname
                        });
                    } else {
                        window.open(link);
                        return;
                    }
                });
            })(btnObj, udButtonId, link, tabname, showType);
        }
    }
    if (objId("live800_video")) {
        UT.addEvent(objId("live800_video"), "click",
        function() {
            if (globalChatHandle.chatStatus == "WAIT" || globalChatHandle.chatStatus == "END") {
                return;
            }
            if (objId('live800_video').style.cursor == "no-drop") {
                return;
            }
            LIM.openVideo(2);
            LIM.vClickOnStatistics.submitInformation(16); //华为点击次数记录
        });
    }
    if (objId('jumperRobot')) {
    	UT.addEvent(objId("jumperRobot"), "click",
        function() {
            if (globalChatHandle.chatStatus == "ROBOT") {
                return;
            }
            if (objId('jumperRobot').style.cursor == "no-drop") {
                return;
            }
            LIM.Robot.jumperRobot();
        });
    }
	if($(".topItemsContainer").length>0){
		$(".topItemsContainer").hover(function(){
			$(this).addClass("top_bg");
		},function(){
			$(this).removeClass("top_bg");
		})
	}
}
var getLeavewordMessageUrl = function() {
    var params = {
        operatorId: LIM.config.operatorId,
        companyID: LIM.config.companyID,
        syslanguage: LIM.config.language
    };
    var infostr = "";
    if (LIM.config["info"] != "") {
        infostr = "&info=" + LIM.config["info"];
    }
    var leaveMesageUrl;
    if (LIM.config['showRightWindow'] == 0) {
        leaveMesageUrl = LIM.config["protocol"] + "://" + LIM.config.chatDirDomains + "/new_leaveMsg.jsp?" + UT.ParamsToUrl(params) + infostr;
    } else {
        leaveMesageUrl = LIM.config["protocol"] + "://" + LIM.config.chatDirDomains + "/leaveMsgPanel.jsp?" + UT.ParamsToUrl(params) + infostr;
    }
    if (LIM.config["leavewordEnable"]) {
        return "";
    }
    return leaveMesageUrl;
};
var urlDecode = function(str) {
    try {
        return decodeURIComponent(str.replace(/\+/g, ' ').replace(/%2B/g, '+'));
    } catch(e) {
        return unescape(str).replace(/\+/g, ' ').replace(/%2B/g, '+');
    }
};
var loadFaq = function() {
    var faq = objId('popular-problem');
    var faqEvent = function(e) {
        var e = e || window.event;
        var el = e.target || e.srcElement;
        var history = document.getElementById("history");
        try {
            var span = el.getElementsByTagName('span')[0];
            var params = {
                "title": urlDecode(el.getAttribute("title")),
                "content": span.innerHTML
            },
            cfgId = el.getAttribute("configid");
            htmlString = "<p style='font-size:14px;font-weight:bold;'>" + urlDecode(el.getAttribute("title")) + "</p><span>" + urlDecode(span.innerHTML) + "</span>";
            globalChatHandle.appendToHistoryBox(globalChatHandle.getHtmlStr('sysinfo',htmlString,'',true));
            faq.className = "";
            el.parentNode.className = "hidden";
            new XHR.request("./scripts/commonQuestionClickAction.jsp?configId=" + cfgId + "&tm=" + new Date().getTime(),
            function() {});
            LIM.vClickOnStatistics.submitInformation(22, cfgId); //华为点击次数记录
        } catch(e) {;
        }
    };
    new XHR.request("scripts/fileCommonPlugin.jsp?companyId=" + LIM.config.companyID + "&configId=" + LIM.config.chatConfigId + "&type=1&t="+new Date().getTime(),
    function() {
        faq.innerHTML = "<span id='faq:arrow' class='arrow'></span>" + this.req.responseText;
        var selected = faq.getElementsByTagName("div")[0],
        options = faq.getElementsByTagName("li"),
        faqlist = faq.getElementsByTagName('ul')[0];
        if (selected && options && faqlist) {
            faqlist.className = 'hidden';
            faqlist.id = "faq-list";
            var length = options.length;
            selected.innerHTML = "&nbsp;"+LIM.resource["changfunc_tip_6"];
            for (var i = 0; i < length; i++) {
                options[i].title = urlDecode(options[i].title);
                UT.addEvent(options[i], "click", faqEvent);
            }
            UT.addEvent(objId('faq:arrow'), 'click',
            function() {
                showList();
            });
            UT.addEvent(objId('faq-current'), 'click',
            function() {
                showList();
            });
            UT.addEvent(objId('faq-current'), 'mouseout',
            function() {
                faqlist.className = 'hidden';
                faq.className = '';
            });
            function showList() {
                faqlist.className = '';
                if (faq.className == 'shown') {
                    faqlist.className = 'hidden';
                    faq.className = '';
                } else {
                    faq.className = 'shown';
                }
            };
            $("#popular-problem>ul").hover(function() {
                faqlist.className = '';
                faq.className = 'shown';
            },
            function() {
                faqlist.className = 'hidden';
                faq.className = '';
            });
        }
    },
    function() {});
};

if (!XHR) var XHR = {};
XHR.request = function(url, onload, onerror, method, params, contentType) {
    this.url = url;
    this.onload = onload;
    this.onerror = (onerror) ? onerror: this.defaultError;
    this.loadXMLDoc(url, method, params, contentType);
};
XHR.request.prototype = {
    loadXMLDoc: function(url, method, params, contentType) {
        if (!method) {
            method = "GET";
        }
        if (!contentType && method == "POST") {
            contentType = "application/x-www-form-urlencoded";
        }
        if (window.XMLHttpRequest) {
            this.req = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            this.req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (this.req) {
            try {
                var request = this;
                this.req.onreadystatechange = function() {
                    request.onReadyState.call(request);
                };
                this.req.open(method, url, true);
                if (contentType) {
                    this.req.setRequestHeader("Content-Type", contentType);
                }
                this.req.send(params);
            } catch(err) {
                this.onerror.call(this);
            }
        }
    },
    onReadyState: function() {
        var req = this.req;
        var ready = req.readyState;
        if (ready == 4) {
            var httpStatus = req.status;
            if (httpStatus == 200 || httpStatus == 0) {
                this.onload.call(this);
            } else {
                this.onerror.call(this);
            }
        }
    },
    defaultError: function() {
        var info = LIM.resource["changfunc_tip_7"] + "<br/>state:" + this.req.readyState + " status:" + this.req.status + " headers:" + this.req.getAllResonseHeaders;
        E.report(info, '1201');
    }
};
if("undefined"==typeof LIM.vClickOnStatistics){
	//华为点击次数记录
	LIM.vClickOnStatistics = {
	    getClickParams:function(itemType,itemId){
	        var params = {
	            operatorId: LIM.config.operatorId?LIM.config.operatorId:null,
	            skillId: LIM.config.skillId?LIM.config.skillId:null,
	            companyID: LIM.config.companyID?LIM.config.companyID:null,
	            itemId: itemId?itemId:null,
	            type: itemType?itemType:null,
	            enterurl: LIM.config.enterurl?LIM.config.enterurl:null
	        };
	        var info = "";
	        if (LIM.config.info != "") {
	            info = "&info=" + LIM.config.info;
	        }
	        var paramstr = UT.ParamsToUrl(params) + info;
	        return paramstr;
	    },
	    getYiClickUrl:function(itemType,itemId) {
	        var url = "./scripts/itemClickAction.jsp?";
	        var param = this.getClickParams(itemType,itemId);
	        url = url + param + "&tm=" + new Date().getTime();
	        return url;
	    },
	    submitInformation:function(type,itemId){
	        var url =  this.getYiClickUrl(type,itemId);
	        new XHR.request(url,function(){});
	    }
	};
}